package com.better.frontend.controller.user;

import com.better.backend.mqReceiverService.UserInfo.model.UserModel;
import com.better.backend.mqReceiverService.UserInfo.repository.UserInfoRepsitory;
import com.better.common.base.ServiceResponse;
import com.better.common.jwtauth.secruity.JWTUserDetails;
import com.better.common.jwtauth.user.User;
import com.better.common.utils.EncryptUtils;
import com.better.common.utils.ServiceResponseHandle;
import com.better.common.utils.SessionUtils;
import com.better.frontend.Iservice.user.UserManagerService;
import com.better.frontend.model.user.UserParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * Created by wfm on 2018/1/6.
 */
@RestController
@RequestMapping("api/user")
@Api(value = "用户管理", tags = {"用户管理接口"})
public class UserManagerController {
    @Autowired
    private UserInfoRepsitory userRepository;
    @Autowired
    private UserManagerService userManagerService;

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @RequestMapping(value = "/modifyPwd", method = RequestMethod.POST)
    public ServiceResponse<?> restPassword(@RequestBody @Valid UserParam userParam) {
        JWTUserDetails user = SessionUtils.getUserInfo();
        UserModel userInfo = userRepository.findByLoginName(user.getUsername());
        if (EncryptUtils.validatePassword(userParam.getOldPassword(), userInfo.getPassword())) {
            userManagerService.updatePasswordById(userInfo.getId(), userInfo.getLoginName(), userParam.getNewPassword());
        } else {
            return ServiceResponseHandle.failed("原密码错误！");
        }
        //todo 后续需要把修改密码同步到后台（等后台采用spring boot 改造后再做处理）
        return ServiceResponseHandle.success("修改密码成功");
    }
}
